#!/bin/bash
#
# Simple script to check syntax erros on
# bash and python scripts
#


function check_errors() {
	local FILENAME=$1
	local TYPE=$(file "$FILENAME" | cut -f2 -d':')
	local STATUS=0
	if [ "$(echo "$TYPE" | grep 'Bourne-Again')" ]; then
		echo "checking bash script $FILENAME ..."
		bash -n "$FILENAME"
		STATUS=$?
	elif [ "$(echo "$TYPE" | grep 'python script')" ]; then
		echo "checking python script $FILENAME ..."
		local OLD_PWD=$PWD
		cd "$(dirname "$FILENAME")"
		local BASENAME=$(basename "$FILENAME")
		python -c "import $(echo "$BASENAME" | cut -f1 -d'.')"
		STATUS=$?
		cd "$OLD_PWD"
	fi

	if [ "$STATUS" -ne 0 ]; then
		echo -e "\n*** ERROR on ${FILENAME}\n"
		return 1
	fi
	return 0
}


echo -e "***   Checking syntax erros in source files ...   ***\n"

find src/ | while read F;
do
	[ "$(echo "$F" | grep '~$')" ] && continue
	[ "$(echo "$F" | grep "\\.svn")" ] && continue
	check_errors $F
done
check_errors "install.sh"
check_errors "uninstall.sh"
